package com.wxxymakers.grademark.config;

import com.wxxymakers.grademark.interceptor.CookieInterceptor;
import com.wxxymakers.grademark.interceptor.GlobalFilter;
import com.wxxymakers.grademark.interceptor.VerifyInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.context.support.ServletRequestHandledEvent;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * @AUTHOR soft
 * @DATE 2018/11/15 0:13
 * @DESCRIBE
 */
@Slf4j
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private CookieInterceptor cookieInterceptor;

    @EventListener(ServletRequestHandledEvent.class)
    public void requestEvent(ServletRequestHandledEvent event) {
        String url     = event.getRequestUrl();
        String method  = event.getMethod();
        String address = event.getClientAddress();
        int    status  = event.getStatusCode();
        if (status == 200) {
            log.info("from: {} {}: {} {}", address, method, url, status);
        } else {
            log.warn("from: {} {}: {} {}", address, method, url, status);
        }
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new VerifyInterceptor())
                .addPathPatterns("/user/none/login", "/user/none/register", "/user/change");
        registry.addInterceptor(cookieInterceptor)
                .addPathPatterns("/*");

        super.addInterceptors(registry);
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        ConverterFactory<String, Enum> converterFactory = new ConverterFactory<String, Enum>() {
            @Override
            public <T extends Enum> Converter<String, T> getConverter(Class<T> targetType) {
                return new Converter<String, T>() {
                    @Override
                    public T convert(String source) {
                        return (T) Enum.valueOf(targetType, source.trim().toUpperCase());
                    }
                };
            }
        };
        registry.addConverterFactory(converterFactory);
        super.addFormatters(registry);
    }

    @Bean
    public FilterRegistrationBean addFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setName("GlobalFilter");
        registration.setFilter(new GlobalFilter());
        registration.addUrlPatterns("/*");
        registration.setOrder(0);
        return registration;
    }
}
